CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12)

FIND_PROGRAM(MKOCTFILE mkoctfile)

IF (MKOCTFILE)

	ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/traxserver.mex COMMAND ${MKOCTFILE}
		-mex -I${CMAKE_SOURCE_DIR}/include/ ${CMAKE_CURRENT_SOURCE_DIR}/traxserver.cpp ${CMAKE_CURRENT_SOURCE_DIR}/helpers.cpp
		-ltraxstatic -DOCTAVE -L${CMAKE_RUNTIME_OUTPUT_DIRECTORY} MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/traxserver.cpp
		DEPENDS
		${CMAKE_CURRENT_SOURCE_DIR}/helpers.cpp
		${CMAKE_CURRENT_SOURCE_DIR}/helpers.h
		traxstatic
		WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} COMMENT "Building traxserver.mex")

	ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/traxclient.mex COMMAND ${MKOCTFILE}
		-mex -I${CMAKE_SOURCE_DIR}/include/ -I${CMAKE_SOURCE_DIR}/support/client/include/
		${CMAKE_CURRENT_SOURCE_DIR}/traxclient.cpp ${CMAKE_CURRENT_SOURCE_DIR}/helpers.cpp
		-ltraxstatic -ltrax_client_static -DOCTAVE -L${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
		-L${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/support/client
		MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/traxclient.cpp
		DEPENDS
			${CMAKE_CURRENT_SOURCE_DIR}/helpers.cpp
			${CMAKE_CURRENT_SOURCE_DIR}/helpers.h
			traxstatic
		WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY} COMMENT "Building traxclient.mex")
	ADD_CUSTOM_TARGET(traxoctave ALL DEPENDS
		${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/traxserver.mex
		${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/traxclient.mex)

	CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/DESCRIPTION.in" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/DESCRIPTION")

	IF (OCTAVE_INSTALL)
		INSTALL(FILES
			"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/traxserver.mex"
			"${CMAKE_CURRENT_SOURCE_DIR}/traxserver.m"
			"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/traxclient.mex"
			"${CMAKE_CURRENT_SOURCE_DIR}/traxclient.m"
			"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/DESCRIPTION" DESTINATION ${OCTAVE_INSTALL}
		)
	ENDIF()

ENDIF()
